<?php

class FilterPanel {

    var $_url;
    var $_fldname;
    var $_fldtampil;
    var $_fldtype;
    var $_fldopt;
    var $_cmdtext = array("%LIKE%" => "SEPERTI", "=" => "=", "!=" => "!=", "LIKE%" => "DIAWALI DENGAN", "%LIKE" => "DIAKHIRI DENGAN");
    var $_cmdinteger = array("=" => "=", "<" => "<", ">" => ">", "!=" => "!=", "BETWEEN" => "DI ANTARA");
    var $_cmdtimestamp = array("=" => "=", "<" => "<", ">" => ">", "!=" => "!=", "BETWEEN" => "DI ANTARA");
    var $_delim = array("AND" => "DAN", "OR" => "ATAU");
    public $is_postgre = false;
    public $is_case_sensitive = false;

    function FilterPanel($url, $fldname, $fldtampil, $fldtype, $fldopt = "") {
        $this->_url = $url;
        $this->_fldname = $fldname;
        $this->_fldtampil = $fldtampil;
        $this->_fldtype = $fldtype;
        $this->_fldopt = $fldopt;
    }

    function getField($fld, $cnt) {
        if (array_key_exists("" . $fld . $cnt . "", $_GET))
            return trim($_GET["" . $fld . $cnt . ""]);
        else
            return "";
    }

    function getFieldType($fld, $cnt) {
        $arrFldTypeCombine = array_combine($this->_fldname, $this->_fldtype);
        return isset($arrFldTypeCombine["" . $this->getField($fld, $cnt) . ""])?$arrFldTypeCombine["" . $this->getField($fld, $cnt) . ""]:"";
    }

    function getWhereTimestamp($namaField, $cmdField, $kriteria1, $kriteria2 = "") {
        $out = "";
        $date1 = GlobalLib::validateDate($kriteria1, (preg_match("#\s#i", $kriteria1) ? true : false));
        $date2 = GlobalLib::validateDate($kriteria2, (preg_match("#\s#i", $kriteria1) ? true : false));

        $kriteria1 = $date1 ? $kriteria1 : Generic::formatDB2Date($kriteria1, (preg_match("#\s#i", $kriteria1) ? true : false));

        $kriteria2 = $date2 ? $kriteria2 : Generic::formatDB2Date($kriteria2, (preg_match("#\s#i", $kriteria1) ? true : false));

        if ($cmdField == "=" || $cmdField == "<" || $cmdField == ">" || $cmdField == "!=") {
            if (trim($kriteria1) != "") {
                if (preg_match("#\s#i", $kriteria1)) {
                    if($this->is_postgre)
                        $out = " date_trunc('second', " . $namaField . ") " . $cmdField . "'" . $kriteria1 . "'::timestamp";
                    else
                        $out = " $namaField " . $cmdField . "'" . $kriteria1 . "'";
                } else {
                    $out = " date(" . $namaField . ") " . $cmdField . "('" . $kriteria1 . "')";
                }
            }
        } else if ($cmdField == "DI ANTARA") {
            if (trim($kriteria1) != "" && trim($kriteria2) != "") {
                if ($kriteria1 > $kriteria2) {
                    if (preg_match("#\s#i", $kriteria1) && preg_match("#\s#i", $kriteria2)) {
                        if($this->is_postgre)
                            $out = " date_trunc('second', " . $namaField . ") BETWEEN '" . $kriteria2 . "'::timestamp AND '" . $kriteria1 . "'::timestamp";
                        else
                            $out = " $namaField BETWEEN '" . $kriteria2 . "' AND '" . $kriteria1 . "'";
                    } else {
                        $out = " date(" . $namaField . ") BETWEEN '" . $kriteria2 . "' AND '" . $kriteria1 . "'";
                    }
                } else {
                    if (preg_match("#\s#i", $kriteria1) && preg_match("#\s#i", $kriteria2)) {
                        if($this->is_postgre)
                            $out = " date_trunc('second', " . $namaField . ") BETWEEN '" . $kriteria1 . "'::timestamp AND '" . $kriteria2 . "'::timestamp";
                        else
                            $out = " $namaField BETWEEN '" . $kriteria1 . "' AND '" . $kriteria2 . "'";
                    } else {
                        $out = " date(" . $namaField . ") BETWEEN '" . $kriteria1 . "' AND '" . $kriteria2 . "'";
                    }
                }
            }
        } else {
            
        }

        $out = trim($out) == "" ? "" : $out;
        return $out;
    }

    function getWhereInteger($namaField, $cmdField, $kriteria1, $kriteria2 = "") {
        $out = "";

        $kriteria1 = !is_numeric(str_replace(array(".", ","), array("", ""), $kriteria1)) ? "0" : str_replace(array(".", ","), array("", ""), $kriteria1);
        $kriteria2 = !is_numeric(str_replace(array(".", ","), array("", ""), $kriteria2)) ? "0" : str_replace(array(".", ","), array("", ""), $kriteria2);
        
        if ($cmdField == "=" || $cmdField == "<" || $cmdField == ">" || $cmdField == "!=") {

            if (trim($kriteria1) != "") {
                $out = $namaField . "" . $cmdField . "" . $kriteria1 . "";
            }
        } else if ($cmdField == "DI ANTARA") {

            if (trim($kriteria1) != "" && trim($kriteria2) != "") {
                if ($kriteria1 > $kriteria2) {
                    $out = $namaField . " BETWEEN " . $kriteria2 . " AND " . $kriteria1 . "";
                } else {
                    $out = $namaField . " BETWEEN " . $kriteria1 . " AND " . $kriteria2 . "";
                }
            }
        } else {
            
        }
        return $out;
        
    }
    
    function getWhereDouble($namaField, $cmdField, $kriteria1, $kriteria2 = "") {
        $out = "";

        $kriteria1 = !is_numeric(str_replace(array(".", ","), array("", ""), $kriteria1)) ? "0" : str_replace(array("," ), array(""), $kriteria1);
        $kriteria2 = !is_numeric(str_replace(array(".", ","), array("", ""), $kriteria2)) ? "0" : str_replace(array(","), array(""), $kriteria2);
        
        if ($cmdField == "=" || $cmdField == "<" || $cmdField == ">" || $cmdField == "!=") {

            if (trim($kriteria1) != "") {
                $out = $namaField . "" . $cmdField . "ROUND(" . $kriteria1 . ")";
            }
        } else if ($cmdField == "DI ANTARA") {

            if (trim($kriteria1) != "" && trim($kriteria2) != "") {
                if ($kriteria1 > $kriteria2) {
                    $out = $namaField . " BETWEEN ROUND(" . $kriteria2 . ") AND ROUND(" . $kriteria1 . ")";
                } else {
                    $out = $namaField . " BETWEEN ROUND(" . $kriteria1 . ") AND ROUND(" . $kriteria2 . ")";
                }
            }
        } else {
            
        }
        return $out;
        
    }

    function getWhereText($namaField, $cmdField, $kriteria1) {
        $out = "";
        
        $field_case_sensitif = preg_match("#id_outlet#i", $namaField) || preg_match("#id_produk#i", $namaField) || preg_match("#bill_info1#i", $namaField) || preg_match("#fullname#i", $namaField);
        
        $field_case_voucher = preg_match("#fullname#i", $namaField);
        
        if($field_case_sensitif)$kriteria1 = strtoupper($kriteria1);
        
        if($this->is_case_sensitive || $field_case_sensitif)
        {
            if ($cmdField == "=" || $cmdField == "!="){
                $out = " (" . $namaField . ") " . $cmdField . " ('" . $kriteria1 . "')";
                }else if ($cmdField == "DIAWALI DENGAN"){
                if($this->is_postgre){
                    $out = " (" . $namaField . ") like(('" . $kriteria1 . "')||'%')";
                }else{
                    $out = " (" . $namaField . ") like('" . $kriteria1 . "%')";
                }
                }else if ($cmdField == "SEPERTI"){
                if($this->is_postgre && $field_case_voucher == ""){
                    $out = " (" . $namaField . ") like('%'||('" . $kriteria1 . "')||'%')";
                }else if ($this->is_postgre && $field_case_voucher != ""){
                    $out = " (" . $namaField . ") like('%" . $kriteria1 . "%')";
                }else{
                    $out = " (" . $namaField . ") like('%" . $kriteria1 . "%')";
                }
                }else if ($cmdField == "DIAKHIRI DENGAN"){
                if($this->is_postgre){
                    $out = " (" . $namaField . ") like('%'||('" . $kriteria1 . "'))";
                }else{
                    $out = " (" . $namaField . ") like('%" . $kriteria1 . "')";
                }
                }else{
                $out = "";
                }
        }
        else
        {
            if ($cmdField == "=" || $cmdField == "!=")
                $out = " (LOWER(" . $namaField . ")) " . $cmdField . " (LOWER('" . $kriteria1 . "'))";
            else if ($cmdField == "DIAWALI DENGAN")
                if($this->is_postgre)
                    $out = " (LOWER(" . $namaField . ")) like((LOWER('" . $kriteria1 . "'))||'%')";
                else
                    $out = " (LOWER(" . $namaField . ")) like(LOWER('" . $kriteria1 . "%'))";
            else if ($cmdField == "SEPERTI")
                if($this->is_postgre)
                    $out = " (LOWER(" . $namaField . ")) like('%'||(LOWER('" . $kriteria1 . "'))||'%')";
                else
                    $out = " (LOWER(" . $namaField . ")) like(LOWER('%" . $kriteria1 . "%'))";
            else if ($cmdField == "DIAKHIRI DENGAN")
                if($this->is_postgre)
                    $out = " (LOWER(" . $namaField . ")) like('%'||(LOWER('" . $kriteria1 . "')))";
                else
                    $out = " (LOWER(" . $namaField . ")) like(LOWER('%" . $kriteria1 . "'))";
            else
                $out = "";
        }

        $outFinal = trim($kriteria1) == "" ? "" : $out;
        $outFinal2 = trim($outFinal) == "" ? "" : $outFinal;
        
        
        return $outFinal2;
    }

    function getFilterString() {
        $whr = "";
        for ($i = 0; $i < 6; $i++) {
            $namaField = $this->getField("src_fld", $i);
            $tipeField = $this->getFieldType("src_fld", $i);
            if ($tipeField == "text") {
                $cmdField = $this->_cmdtext[$this->getField("src_cmd", $i)];
            } else if ($tipeField == "integer") {
                $cmdField = $this->_cmdinteger[$this->getField("src_cmd", $i)];
            } else if ($tipeField == "double") {
                $cmdField = $this->_cmdinteger[$this->getField("src_cmd", $i)];
            } else if ($tipeField == "timestamp") {
                $cmdField = $this->_cmdtimestamp[$this->getField("src_cmd", $i)];
            }

            $kriteria1 = $this->getField("src_val_start", $i);
            $kriteria2 = $this->getField("src_val_end", $i);
            $delim = $this->getField("src_dlm", $i - 1);

            if ($tipeField == "text") {
                $out = $this->getWhereText($namaField, $cmdField, $kriteria1);
            } else if ($tipeField == "integer") {
                $out = $this->getWhereInteger($namaField, $cmdField, $kriteria1, $kriteria2);
            } else if ($tipeField == "double") {
                $out = $this->getWhereDouble($namaField, $cmdField, $kriteria1, $kriteria2);
            } else if ($tipeField == "timestamp") {
                $out = $this->getWhereTimestamp($namaField, $cmdField, $kriteria1, $kriteria2);
            }

            if (trim($kriteria1) != "" && $out != "") {
                if($whr == "")
                    $whr .= "(" . $out;
                else if($out != "") {
                    if($delim == "OR") //$pre_namaField == $namaField && 
                        $whr .= " " . $delim . " " . $out;
                    else
                        $whr .= " ) " . $delim . " ( " . $out;
                }
            }
            
        }
        
        if($whr != "")
            $whr .= ")";
        
        return $whr;
    }
    
    
    function getFilterStringSingle() {
        $whr = "";
        for ($i = 0; $i < 1; $i++) {
            $namaField = $this->getField("src_fld", $i);
            $tipeField = $this->getFieldType("src_fld", $i);

            if ($tipeField == "text")
                $cmdField = $this->_cmdtext[$this->getField("src_cmd", $i)];
            else if ($tipeField == "integer")
                $cmdField = $this->_cmdinteger[$this->getField("src_cmd", $i)];
            else if ($tipeField == "double")
                $cmdField = $this->_cmdinteger[$this->getField("src_cmd", $i)];
            else if ($tipeField == "timestamp")
                $cmdField = $this->_cmdtimestamp[$this->getField("src_cmd", $i)];

            $kriteria1 = $this->getField("src_val_start", $i);
            $kriteria2 = $this->getField("src_val_end", $i);
            $delim = $this->getField("src_dlm", $i - 1);
            $delim = " AND ";

            if ($tipeField == "text")
                $out = $this->getWhereText($namaField, $cmdField, $kriteria1);
            else if ($tipeField == "integer")
                $out = $this->getWhereInteger($namaField, $cmdField, $kriteria1, $kriteria2);
            else if ($tipeField == "double")
                $out = $this->getWhereDouble($namaField, $cmdField, $kriteria1, $kriteria2);
            else if ($tipeField == "timestamp")
                $out = $this->getWhereTimestamp($namaField, $cmdField, $kriteria1, $kriteria2);
            else {
                
            }


            $whr .= $whr == "" ? $out : ($out == "" ? "" : " " . $delim . " " . $out);
        }


        return $whr;
    }

    function drawfilterpanelsingle() {
        $showSrcValEnd = "";

        $fld = array_combine($this->_fldname, $this->_fldtampil);

        $out = "";
        $out.= $this->changeCmdSrc();

        if (count($this->_fldname)) {
            $_cnt = 5;

            for ($_cnt; $_cnt < 6; $_cnt++) 
            {
                if(isset($_GET["src_cmd" . $_cnt]))
                {
                    if (array_key_exists($_GET["src_cmd" . $_cnt], $this->_cmdtext)) {
                        $cmd = array("" . $_GET["src_cmd" . $_cnt] . "" => "" . $this->_cmdtext[$_GET["src_cmd" . $_cnt]] . "");
                    } else if (array_key_exists($_GET["src_cmd" . $_cnt], $this->_cmdinteger)) {
                        $cmd = array("" . $_GET["src_cmd" . $_cnt] . "" => "" . $this->_cmdinteger[$_GET["src_cmd" . $_cnt]] . "");
                    } else if (array_key_exists($_GET["src_cmd" . $_cnt], $this->_cmdtimestamp)) {
                        $cmd = array("" . $_GET["src_cmd" . $_cnt] . "" => "" . $this->_cmdtimestamp[$_GET["src_cmd" . $_cnt]] . "");
                    } else {
                        $cmd = array("=" => "=");
                    }
                } else {
                    $cmd = array("=" => "=");
                }

                $showSrcValEnd = isset($_GET["src_val_end" . $_cnt]) && trim($_GET["src_val_end" . $_cnt]) != "" ? "" : "style=\"display:none;\"";

                $out .= "" . Generic::createComboArray2("src_fld" . $_cnt, $fld, (isset($_GET["src_fld" . $_cnt])?$_GET["src_fld" . $_cnt]:"")) . "";
                $out .= "&nbsp;&nbsp;" . Generic::createComboArray2("src_cmd" . $_cnt, $cmd, (isset($_GET["src_cmd" . $_cnt])?$_GET["src_cmd" . $_cnt]:"")) . "";
                $out .= "&nbsp;&nbsp;<input type=\"text\" id=\"src_val_start" . $_cnt . "\" name=\"src_val_start" . $_cnt . "\" value=\"" . (isset($_GET["src_val_start" . $_cnt])?$_GET["src_val_start" . $_cnt]:"") . "\" />";
                $out .= "&nbsp;<input $showSrcValEnd type=\"text\" id=\"src_val_end" . $_cnt . "\" name=\"src_val_end" . $_cnt . "\" value=\"" . (isset($_GET["src_val_end" . $_cnt])?$_GET["src_val_end" . $_cnt]:"") . "\" />";
                $out .= "&nbsp;<input type=\"submit\" name=\"src_submit\" id=\"src_submit\" value=\"Cari\" />";
            }
        }


        return $out;
    }
    
   

    function drawfilterpanel() {
        $showSrcValEnd = "";

        $fld = array_combine($this->_fldname, $this->_fldtampil);

        $out = "";

        if (count($this->_fldname)) {
            $_cnt = 0;
            $filled_field_count = 0;
            
            if(is_array($this->_fldopt)){
                foreach($this->_fldopt as $key => $row){
                    if (isset($_GET["check_" . $key])) {
                        $filled_field_count++;
                    }
                    
                    $out .= "<tr class=\"tr_slide\">";
                    $out .= "<td><input type=\"checkbox\" id=\"check_$key\" name=\"check_$key\" ".($_GET["check_$key"] ? "checked=\"checked\"" : "")." value=1>&nbsp;$row&nbsp;";
                    $out .= "&nbsp;&nbsp;=";
                    $out .= "&nbsp;&nbsp;<input type=\"text\" id=\"src_val_start" . (6+$key) . "\" name=\"src_val_start" . (6+$key) . "\" value=\"" . $_GET["src_val_start" . (6+$key)] . "\" />";
                    $out .= "</tr>";
                }
            }
            
            for ($_cnt; $_cnt < 5; $_cnt++) {
                if (isset($_GET["src_val_start" . $_cnt])) {
                    if ($_GET["src_val_start" . $_cnt] != "") {
                        $filled_field_count++;
                    }
                }

                if (isset($_GET["src_cmd" . $_cnt]) && array_key_exists($_GET["src_cmd" . $_cnt], $this->_cmdtext)) {
                    $cmd = array("" . $_GET["src_cmd" . $_cnt] . "" => "" . $this->_cmdtext[$_GET["src_cmd" . $_cnt]] . "");
                } else if (isset($_GET["src_cmd" . $_cnt]) && array_key_exists($_GET["src_cmd" . $_cnt], $this->_cmdinteger)) {
                    $cmd = array("" . $_GET["src_cmd" . $_cnt] . "" => "" . $this->_cmdinteger[$_GET["src_cmd" . $_cnt]] . "");
                } else if (isset($_GET["src_cmd" . $_cnt]) && array_key_exists($_GET["src_cmd" . $_cnt], $this->_cmdtimestamp)) {
                    $cmd = array("" . $_GET["src_cmd" . $_cnt] . "" => "" . $this->_cmdtimestamp[$_GET["src_cmd" . $_cnt]] . "");
                } else {
                    $cmd = array("=" => "=");
                }

                $showSrcValEnd = isset($_GET["src_val_end" . $_cnt]) && trim($_GET["src_val_end" . $_cnt]) != "" ? "" : "style=\"display:none;\"";

                $out .= "<tr class=\"tr_slide\">";
                $out .= "<td>" . Generic::createComboArray2("src_fld" . $_cnt, $fld, (isset($_GET["src_fld" . $_cnt])?$_GET["src_fld" . $_cnt]:""));
                $out .= "&nbsp;&nbsp;" . Generic::createComboArray2("src_cmd" . $_cnt, $cmd, (isset($_GET["src_cmd" . $_cnt])?$_GET["src_cmd" . $_cnt]:""));
                $out .= "&nbsp;&nbsp;<input type=\"text\" id=\"src_val_start" . $_cnt . "\" name=\"src_val_start" . $_cnt . "\" value=\"" . (isset($_GET["src_val_start" . $_cnt])?$_GET["src_val_start" . $_cnt]:"") . "\" />";
                $out .= "&nbsp;<input $showSrcValEnd type=\"text\" id=\"src_val_end" . $_cnt . "\" name=\"src_val_end" . $_cnt . "\" value=\"" . (isset($_GET["src_val_end" . $_cnt])?$_GET["src_val_end" . $_cnt]:"") . "\" />";
                $out .= "&nbsp;" . Generic::createComboArray2("src_dlm" . $_cnt, $this->_delim, (isset($_GET["src_dlm" . $_cnt])?$_GET["src_dlm" . $_cnt]:"")) . "</td>";
                $out .= "</tr>";
            }
            
            $is_form_open = isset($_GET["is_form_open"])?$_GET["is_form_open"]:"false";
            
            if ($filled_field_count >= 1 || $is_form_open == 'true') {
                $out .= "
                    <script>
                        jQuery(function(){
                            setTimeout(function(){
                                jQuery('.tr_slide').show();
                                jQuery('#fCari').attr('action', '?is_form_open=true');
                            }, 50)
                        })
                    </script>
                ";
            }
        }

        return $out;
    }

    function toArrayJS($arr) {
        $arrcmdtext = "";
        $bukak = "";
        $fldtype = "";
        $tutup = "";
        if (is_array($arr)) {
            $fldtype = "";
            $bukak = "[";
            foreach ($arr as $key => $value) {
                $delim = ",";
                $fldtype .= $fldtype == "" ? "['$key', '$value']" : $delim . "['$key', '$value']";
            }
            $tutup = "]";
        }
        $arrcmdtext = $bukak . $fldtype . $tutup . ";";
        return $arrcmdtext;
    }

    function toArrayJS2($arr) {
        $arrtipefield = "";
        $bukak = "";
        $fldtype = "";
        $tutup = "";
        if (is_array($arr)) {
            $fldtype = "";
            $bukak = "[";
            foreach ($arr as $value) {
                $delim = ",";
                $fldtype .= $fldtype == "" ? "'$value'" : $delim . "'$value'";
            }
            $tutup = "]";
        }

        $arrtipefield = $bukak . $fldtype . $tutup . ";";
        return $arrtipefield;
    }

    function changeCmdSrc() {

        $out = "";
        $out .= "
            <script type=\"text/javascript\">
                jQuery(document).ready(function(){
                    var arrtipefield = " . $this->toArrayJS2($this->_fldtype) . "
                    var arrtext = " . $this->toArrayJS($this->_cmdtext) . "
                    var arrinteger = " . $this->toArrayJS($this->_cmdinteger) . "
                    var arrtimestamp = " . $this->toArrayJS($this->_cmdtimestamp) . "
                    var tipe = '';
                    var selectedIndex = '';
                    var elemCbxFldIdx='';
                    var elemCbxCmdIdx='';
                    var elemCbxCmd='';
                    var elemTxtKriteria2='';";
        $out .= "
                    jQuery('select[name*=\"src_fld\"]').click(function() {
                            elemCbxFldIdx = jQuery(this).attr('id');
                            jQuery('option:selected', this).each(function() {
                                    selectedIndex = jQuery(this).index();
                                    elemCbxCmd = jQuery('select[name=\"src_cmd'+ elemCbxFldIdx[7] +'\"]');
                                    tipe = arrtipefield[selectedIndex];
                                    elemCbxCmd.empty();
                                    if(tipe == 'text'){
                                            jQuery.each(arrtext, function(kunci, nilai) {
                                                    elemCbxCmd.append(new Option(nilai[1], nilai[0]));
                                            });
                                    }else if(tipe == 'integer'){
                                            jQuery.each(arrinteger, function(kunci, nilai) {
                                                    elemCbxCmd.append(new Option(nilai[1], nilai[0]));
                                            });
                                    }else if(tipe == 'timestamp'){
                                            jQuery.each(arrtimestamp, function(kunci, nilai) {
                                                    elemCbxCmd.append(new Option(nilai[1], nilai[0]));
                                            });
                                    }

                                    objtxtkriteria = jQuery('input[name=\'src_val_end'+ elemCbxFldIdx[7] +'\']');
                                    cbxcmdtext = elemCbxCmd.text();
                                    if(cbxcmdtext == 'BETWEEN'){
                                        objtxtkriteria.show();
                                    }else{
                                        objtxtkriteria.hide();
                                        objtxtkriteria.attr('value','');
                                    }


                            });
                    });";
        $out .= "
                    jQuery('select[name*=\"src_cmd\"]').change(                            
                            function(){
                                elemCbxCmdIdx = jQuery(this).attr('id');
                                jQuery('option:selected', this).each(
                                    function() {
                                        objtxtkriteria = jQuery('input[name=\'src_val_end'+ elemCbxCmdIdx[7] +'\']');                                        
                                        if(jQuery(this).val() == 'BETWEEN'){
                                                objtxtkriteria.show();
                                        }else{
                                                objtxtkriteria.hide();
                                                objtxtkriteria.attr('value','');
                                        }
                                    }
                                 )
                            }
                    ).trigger('change');";

        $out .="});";

        $out.= "</scr" . "ipt>";
        return $out;
    }

}

?>